package com.runfast.waimai.web.controller;

import com.runfast.common.utils.SessionContext;
import com.runfast.common.web.entity.Result;
import com.runfast.waimai.dao.model.*;
import com.runfast.waimai.service.RunfastBusinessService;
import com.runfast.waimai.service.RunfastBusinessTypeService;
import com.runfast.waimai.service.RunfastHomepageService;
import com.runfast.waimai.service.WaiMaiRunfastAgentbusinessService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ${DESCRIPTION}  外卖首页模块管理
 *
 * @author chenzheng
 * @create 2018-05-21 14:39
 **/
@RestController("homePageController")
@RequestMapping("/server/homePage")
public class HomePageController {
    @Autowired
    private RunfastHomepageService runfastHomepageService;
    @Autowired
    private WaiMaiRunfastAgentbusinessService waiMaiRunfastAgentbusinessService;
    @Autowired
    private RunfastBusinessTypeService runfastBusinessTypeService;
    @Autowired
    private RunfastBusinessService runfastBusinessService;
    /**
     * 外卖首页模块列表带分页的
     */
    @GetMapping("/list")
    public Result list(Integer agentid,String name,Integer typelink){
        BaseUser admin = SessionContext.getAdmin();
        WaiMaiRunfastAgentaccounts agentAccounts= SessionContext.getCurrentRunfastAgentaccounts();
        RunfastBusinessAccounts accounts = SessionContext.getRunfastBusinessAccounts();
        RunfastHomepageExample example=new RunfastHomepageExample();
        example.setOrderByClause("sort  asc ");
        RunfastHomepageExample.Criteria  exampleCriteria=example.createCriteria();
        if(agentid!=null){
            exampleCriteria.andAgentidEqualTo(agentid);
        }
        if(StringUtils.isNotBlank(name)){
            exampleCriteria.andNameLike("%"+name+"%");
        }
        if(typelink!=null){
            exampleCriteria.andTypelinkEqualTo(typelink);
        }
        if(admin!=null && admin.getType()==2 && agentAccounts!=null){
            exampleCriteria.andAgentidEqualTo(agentAccounts.getAgentid());
        }else if(admin!=null && admin.getType()==1){
        }
        List<RunfastHomepage> list=runfastHomepageService.selectByExample(example);
        Integer total=runfastHomepageService.countByExample(example);
        Map<Object,Object> map=new HashMap<Object, Object>();
        map.put("list",list);
        map.put("total",total);
        return Result.ok(null,map);
    }

    /**
     * 外卖首页模块列表
     */
   /* @GetMapping("/select")
    //@RequiresPermissions("server:goodsSellType:select")
    public Result select(){
        List<RunfastGoodsSellType> list = runfastGoodsSellTypeService.selectByExample(null);
        return Result.ok(null,list);
    }*/

    /**
     * 保存外卖首页模块
     */
    @PostMapping("/save")
    public Result save(@RequestBody RunfastHomepage homepage){
        if(homepage!=null &&homepage.getAgentid()!=null && homepage.getTypelink()!=null && StringUtils.isNotBlank(homepage.getIcon()) && StringUtils.isNotBlank(homepage.getName())){
            WaiMaiRunfastAgentbusiness  agentbusiness=waiMaiRunfastAgentbusinessService.selectByPrimaryKey(homepage.getAgentid());
            if(agentbusiness!=null){
                homepage.setAgentid(agentbusiness.getId());
                homepage.setAgentname(agentbusiness.getName());
                homepage.setCreatetime(new Date());
                homepage.setWill(1);
                if(homepage.getTypelink()==1&& homepage.getCommonid()!=null){
                    //商家分类
                    RunfastBusinessType businessType=runfastBusinessTypeService.selectByPrimaryKey(homepage.getCommonid());
                    if(businessType!=null){
                        homepage.setTypename(businessType.getName());
                    }else{return Result.fail(null,"找不到对应的商家分类！");}

                }else if(homepage.getTypelink()==2 && homepage.getCommonid()!=null){
                    // 商家
                    RunfastBusiness business=runfastBusinessService.selectByPrimaryKey(homepage.getCommonid());
                    if(business!=null){
                        homepage.setTypename(business.getName());
                    }else{return Result.fail(null,"找不到对应的商家！");}
                }else if(homepage.getTypelink()==5){
                        homepage.setTypename("跑腿服务");
                }
                RunfastHomepageExample example=new RunfastHomepageExample();
                example.createCriteria().andAgentidEqualTo(agentbusiness.getId());
                Integer total=runfastHomepageService.countByExample(example);
                homepage.setSort(total);
                RunfastHomepageExample example1=new RunfastHomepageExample();
                example1.createCriteria().andAgentidEqualTo(agentbusiness.getId());
                Integer total1=runfastHomepageService.countByExample(example1);
                if(total1>20){
                      return  Result.fail(null,"不能超过20条！");
                }
                runfastHomepageService.insertSelective(homepage);
            }else{return Result.fail(null,"找不到对应的代理商！");}
        }else{return Result.fail(null,"传入参数不对！");}
        return Result.ok(null);
    }

    /**
     * 修改外卖首页模块
     */
    @PostMapping("/update")
    public Result update(@RequestBody RunfastHomepage homepage){
        if(homepage!=null &&homepage.getAgentid()!=null && homepage.getTypelink()!=null && StringUtils.isNotBlank(homepage.getIcon()) && StringUtils.isNotBlank(homepage.getName())){
            WaiMaiRunfastAgentbusiness  agentbusiness=waiMaiRunfastAgentbusinessService.selectByPrimaryKey(homepage.getAgentid());
            if(agentbusiness!=null){
                homepage.setAgentid(agentbusiness.getId());
                homepage.setAgentname(agentbusiness.getName());
                if(homepage.getTypelink()==1&& homepage.getCommonid()!=null){
                    //商家分类
                    RunfastBusinessType businessType=runfastBusinessTypeService.selectByPrimaryKey(homepage.getCommonid());
                    if(businessType!=null){
                        homepage.setTypename(businessType.getName());
                    }else{return Result.fail(null,"找不到对应的商家分类！");}

                }else if(homepage.getTypelink()==2 && homepage.getCommonid()!=null){
                    // 商家
                    RunfastBusiness business=runfastBusinessService.selectByPrimaryKey(homepage.getCommonid());
                    if(business!=null){
                        homepage.setTypename(business.getName());
                    }else{return Result.fail(null,"找不到对应的商家！");}
                }
                runfastHomepageService.updateByPrimaryKeySelective(homepage);
            }else{return Result.fail(null,"找不到对应的代理商！");}
        }else{return Result.fail(null,"传入参数不对！");}
        return Result.ok(null);
    }

    /**
     * 删除外卖首页模块
     */
    @GetMapping("/delete")
    public Result delete(Integer id){
        if(id!=null){
            runfastHomepageService.deleteByPrimaryKey(id);
        }else{return Result.fail(null,"传入参数不对！");}
        return Result.ok(null);
    }

    /**拖动排序*/
    @PostMapping("/dragSort")
    public Result dragSort(@RequestBody RunfastHomepage homepage){
        if(homepage!=null && homepage.getIds()!=null &&homepage.getAgentid()!=null){
            int i=0;
            for(Integer in:homepage.getIds()){
                RunfastHomepage homepage1=new RunfastHomepage();
                homepage1.setId(in);
                homepage1.setSort(i);
                runfastHomepageService.updateByPrimaryKeySelective(homepage1);
                i++;
            }
        }else{return Result.fail(null,"传入参数不对！");}
        return Result.ok(null);
    }



}
